//
//  DiamondBuyGoodsCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/2.
//

import UIKit

class DiamondBuyGoodsCell: UITableViewCell {
    var dataSource:[ProductModel] = []
    var chooseProduct:( (ProductModel) -> Void)?
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textAlignment = .center
        label.text = "钻石充值"
        return label
    }()
    
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: DiamondGoodsItem.self)
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        self.contentView.addSubview(titleLb)
        self.contentView.addSubview(collectionView)
        titleLb.snp.makeConstraints { make in
            make.top.equalTo(30)
            make.leading.equalTo(14)
            make.height.equalTo(20)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLb.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    func setup(models:[ProductModel]){
        self.dataSource = models
        self.collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension DiamondBuyGoodsCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = dataSource[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withClass: DiamondGoodsItem.self, for: indexPath)
        cell.contentView.layer.borderColor = UIColor(hexString: "#45FFFF").cgColor
        cell.createAttributedText(money: model.days ?? "")
        cell.moneyLb.text = "¥\(model.price ?? 0)"
        cell.discountsLb.isHidden = true
        if model.isSelect {
            cell.contentView.layer.borderWidth = 1
        }else {
            cell.contentView.layer.borderWidth = 0
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (SCREEN_WIDTH - 48) / 3
        return CGSize(width: Int(width), height: Int(GET_WIDTH(W: 90)))
    }
    
    //上下间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    //左右间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.chooseProduct?(dataSource[indexPath.item])
    }
    
}

class DiamondGoodsItem: UICollectionViewCell {
      
    lazy var numberLb: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = UIFont.pingFangSCSemibold(ofSize: 20)
        label.textAlignment = .center
        label.text = "0"
        return label
    }()
    
    lazy var moneyLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#FFFFFF", transparency: 0.5)
        label.font = UIFont.pingFangSCRegular(ofSize: 14)
        label.textAlignment = .center
        label.text = "$100"
        return label
    }()
    
    lazy var discountsLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#5C1A1A")
        label.backgroundColor = UIColor(hexString: "#FFB056")
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        label.text = "  超划算  "
        label.corners([.layerMaxXMaxYCorner, .layerMinXMinYCorner], cornerRadius: 8)
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        contentView.backgroundColor = UIColor(hexString: "#FFFFFF", transparency: 0.1)
        contentView.allRound(10)
        contentView.addSubviews([moneyLb, numberLb, discountsLb])
        
        numberLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview().offset(-6)
            make.centerX.equalToSuperview()
        }
        
        moneyLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.numberLb.snp.bottom).offset(4)
        }
        
        discountsLb.snp.makeConstraints { make in
            make.leading.top.equalToSuperview()
            make.height.equalTo(20)
        }
    }
    
    func createAttributedText(money:String) {
        // 创建 NSMutableAttributedString
        let attributedText = NSMutableAttributedString(string: " \(money)")
        // 创建并添加图片
        if let image = UIImage(named: "icon_mine_diamond") { // 使用系统图标作为示例
            let imageAttachment = NSTextAttachment()
            imageAttachment.image = image
            imageAttachment.bounds = CGRect(x: 0, y: 2, width: 18, height: 14) // 调整图片大小和位置
            attributedText.insert(NSAttributedString(attachment: imageAttachment), at: 0)
        }
        self.numberLb.attributedText = attributedText
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
